#include <iostream>
#include <string.h>
#include <stack>
using namespace std;

const int N = 1e4 + 7;
int dep[N], pre[N];
bool vis[N];
int ep, n, rsc;

struct EDGE
{
    int s, e;
    EDGE *next;
} edges[N * N];

struct NODE
{
    EDGE *first;
} nodes[N];

void addedge(int s, int e)
{
    edges[ep].s = s;
    edges[ep].e = e;
    edges[ep].next = nodes[s].first;
    nodes[s].first = &edges[ep];
    ep++;
}

void dijstra(int rsc)
{
    memset(dep, 0, sizeof(dep));
    memset(vis, false, sizeof(vis));
    memset(pre, -1, sizeof(pre));
    dep[rsc] = 1;
    while (1)
    {
        int pos = -1, maxd = 0;
        for (int i = 0; i < n; i++)
        {
            if (!vis[i] && dep[i] > maxd)
            {
                maxd = dep[i];
                pos = i;
            }
        }
        if (pos == -1)
        {
            break;
        }
        vis[pos] = true;
        EDGE *e = nodes[pos].first;
        while (e != NULL)
        {
            if (dep[e->e] < dep[e->s] + 1)
            {
                dep[e->e] = dep[e->s] + 1;
                pre[e->e] = e->s;
            }
            e = e->next;
        }
    }
}

int cmp(int a, int b)
{
    int pos1 = a;
    int pos2 = b;
    stack<int> st1;
    stack<int> st2;
    while (pos1 != -1 && pos2 != -1)
    {
        st1.push(pos1);
        st2.push(pos2);
        pos1 = pre[pos1];
        pos2 = pre[pos2];
    }
    while (!st1.empty() && !st2.empty())
    {
        if (st1.top() != st2.top())
        {
            if (st1.top() < st2.top())
            {
                return a;
            }
            else
            {
                return b;
            }
        }
        st1.pop();
        st2.pop();
    }
    return b;
}

int main()
{
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        int cnt;
        cin >> cnt;
        for (int j = 0; j < cnt; j++)
        {
            int next;
            cin >> next;
            addedge(i, next);
            vis[next] = true;
        }
    }
    for (int i = 0; i < n; i++)
    {
        if (!vis[i])
        {
            rsc = i;
        }
    }
    dijstra(rsc);
    int maxd = 0, pos = 0;
    for (int i = 0; i < n; i++)
    {
        if (dep[i] > maxd)
        {
            maxd = dep[i];
            pos = i;
        }
        else if (dep[i] == maxd)
        {
            // 相同长度情况下要比较二者的全部路径，从头到尾找到较小数列
            pos = cmp(i, pos);
        }
    }
    stack<int> st;
    cout << maxd << endl;
    while (pos != -1)
    {
        st.push(pos);
        pos = pre[pos];
    }
    int flag = 0;
    while (!st.empty())
    {
        if (flag)
        {
            cout << " ";
        }
        cout << st.top();
        st.pop();
        flag = 1;
    }
    return 0;
}